package com.bwie.auth.controller;

import com.bwie.auth.service.AuthService;
import com.bwie.common.domain.request.UserReq;
import com.bwie.common.result.Result;
import com.bwie.common.utils.ImageVerificationCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.OutputStream;
import java.util.concurrent.TimeUnit;

/**
 * @className: AuthController
 * @Description: TODO
 * @version: v1.８.0
 * @author:
 * @date:
 */
@RestController
public class AuthController {
    @Autowired
    private AuthService authService;
    @Autowired
    private RedisTemplate redisTemplate;

    @PostMapping("/login")
    public Result<String> login(@RequestBody UserReq userReq){
        String token = authService.login(userReq);
        return Result.success(token);
    }

    /**
     * t图片验证码
     * @param outputStream
     */
    @GetMapping("/imgCode")
    public void imgCode( OutputStream outputStream){
        ImageVerificationCode verificationCode = new ImageVerificationCode();
        BufferedImage image = verificationCode.getImage();
        String text = verificationCode.getText();
        //图片验证码换成到redis
        redisTemplate.opsForValue().set(text,1,3, TimeUnit.MINUTES);

        //图片响应到前台
        try {
            ImageVerificationCode.output(image,outputStream);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }

    }

}
